package hlhj.fhp.tvlib.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.bumptech.glide.Glide
import com.bumptech.glide.load.Option
import com.bumptech.glide.load.Options
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.GoLoginDialog
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.BaseBean
import hlhj.fhp.tvlib.javabean.CommentListBean
import hlhj.fhp.tvlib.javabean.LiveDetailBean
import hlhj.fhp.tvlib.utils.TimeUt
import kotlinx.android.synthetic.main.live_fgm_comment.view.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\5\24 0024.
 */
class LiveCommentFgm :Fragment(), SpringView.OnFreshListener {
    private lateinit var rootView:View
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var adp:BaseQuickAdapter<CommentListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<CommentListBean.DataBean>()
    private var lid=""
    private var page=1
    companion object {
        fun getInstence(id:String): LiveCommentFgm {
            val fgm = LiveCommentFgm()
            val bundle = Bundle()
            bundle.putString("id",id)
            fgm.arguments=bundle
            return fgm
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.live_fgm_comment,null)
        lid=arguments?.getString("id")!!
        loadingDialog=LoadingDialog(context!!)
        setComment()
        initRcy()
        initSp()
        initButton()
        return rootView
    }

    private fun setComment() {
        OkGo.get<String>(Contents.BaseUrl + Contents.LIVE_DETAIL)
                .params("live_id", lid)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val str = response?.body()
                        val detailBean = Gson().fromJson(str, LiveDetailBean::class.java)
                        if (detailBean.code==1){
                            if (detailBean.data.comment_status==1){
                                rootView.bottomBar.visibility=View.VISIBLE
                            }else if (detailBean.data.comment_status==2){
                                rootView.bottomBar.visibility=View.GONE
                            }
                        }
                    }
                })
    }

    private fun initSp() {
        rootView.spView.header=DefaultHeader(context)
        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.setListener(this)
    }

    private fun initRcy() {
        adp=object :BaseQuickAdapter<CommentListBean.DataBean,BaseViewHolder>(R.layout.live_fgm_comment_item,datas){
            override fun convert(helper: BaseViewHolder?, item: CommentListBean.DataBean?) {
                val options = RequestOptions()
                options.placeholder(R.drawable.ic_zb_touxiang)
                        .error(R.drawable.ic_zb_touxiang)
                Glide.with(mContext).load(Contents.IMG_URL+item?.head_pic!!.replace("\\","/"))
                        .apply(options)
                        .into(helper?.getView(R.id.itemIcon)!!)
                helper?.setText(R.id.itemName,item?.user_name)
                helper?.setText(R.id.itemContent,item?.content)
                helper?.setText(R.id.itemTime,TimeUt.timeStampToStr(item?.create_at.toLong()))

            }
        }
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        getData()
    }

    private fun initButton() {
        rootView.btSend.setOnClickListener {
            if (rootView.etContent.text.isEmpty())return@setOnClickListener
            if (TMSharedPUtil.getTMToken(context).equals("")|| TMSharedPUtil.getTMToken(context)==null){
                GoLoginDialog(context!!).show()
            }else{
                OkGo.get<String>(Contents.BaseUrl+Contents.COMMENT)
                        .params("live_id",lid)
                        .params("content",rootView.etContent.text.toString())
                        .params("token",TMSharedPUtil.getTMToken(context!!))
                        .execute(object :StringCallback(){
                            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                                super.onStart(request)
                                loadingDialog.show()

                            }
                            override fun onSuccess(response: Response<String>?) {
                                loadingDialog.dismiss()
                                val str = response?.body()
                                val baseBean = Gson().fromJson(str, BaseBean::class.java)
                                if (baseBean.code==1){
                                    Toast.makeText(context,"评论成功",Toast.LENGTH_SHORT).show()
                                    rootView.etContent.setText("")
                                    datas.clear()
                                    getData()
                                }else{
                                    Toast.makeText(context,baseBean.msg,Toast.LENGTH_SHORT).show()
                                }
                            }
                        })
            }

        }
    }

    fun getData(){
        OkGo.get<String>(Contents.BaseUrl+Contents.COMMENT_LIST)
                .params("live_id",lid)
                .params("page",page)
                .execute(object :StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        rootView.spView.onFinishFreshAndLoad()
                        loadingDialog.dismiss()
                        val str = response?.body()
                        val listBean = Gson().fromJson(str, CommentListBean::class.java)
                        if (listBean.code==1){
                            datas.addAll(listBean.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
    }

    override fun onLoadmore() {
        page++
        getData()
    }

    override fun onRefresh() {
        page=1
        datas.clear()
        getData()
    }
}